﻿using Quartz;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace LL2025.TimeTask.Lib
{
    public class MyJob : IJob
    {
        public Random m_random = new Random();
        public Task Execute(IJobExecutionContext context) // Changed Task? to Task to match the IJob interface
        {
            int a = m_random.Next(0, 100);
            int b = m_random.Next(0, 100);

            Console.WriteLine($"{DateTime.Now.ToString()}:{a} + {b} = {a + b}");

            return Task.CompletedTask; // Return a completed Task to ensure proper implementation
        }
    }

    //运行并发运行
    public class MyFirstJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            //JobDetail.Key：标识JobDetail的唯一ID
            //Trigger.Key：标识Trigger的唯一ID
            Console.WriteLine($"1,现在时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}，" +
                $"工作明细：{context.JobDetail.Key.Name}，工作触发器：{context.Trigger.Key.Name}");
            return Task.CompletedTask; // Return a completed Task to ensure proper implementation

        }
    }
    //运行并发运行
    public class MySecondJob : IJob
    {
        //获得值的方式：3、属性注入
        public string Cnxy { set; get; }
        public Task Execute(IJobExecutionContext context)
        {
            //获得值的方式：1、通过IJobExecutionContext.JobDetail.JobDataMap["Key"]获得所对应的值
            string value1 = (string)context.JobDetail.JobDataMap["Title"];
            //获得值的方式：2、也可以通过IJobExecutionContext.MergedJobDataMap["Key"]获得所对应的值
            double value2 = 1;
            Console.WriteLine($"2,现在时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}，" +
                $"Title：{value1}，Pi：{value2}，CNXY：{Cnxy}");
            return Task.CompletedTask; // Return a completed Task to ensure proper implementation

        }
    }

    //不运行并发运行
    [DisallowConcurrentExecution]
    public class MyThridJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            Console.WriteLine($"3,现在时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
            Thread.Sleep(3);
            return Task.CompletedTask; // Return a completed Task to ensure proper implementation

        }
    }

}
